define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'metting/index',
                    add_url: 'metting/index/add',
                    edit_url: 'metting/index/edit',
                    del_url: 'metting/index/del',
                    multi_url: 'metting/index/multi',
                    table: 'metting',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'community', title: __('Community'), formatter: function (community) {
                                if(community) {
                                    return community.name;
                                }
                                return '';
                            }},
                        {field: 'room_name', title: "会议室名称"},
                        {field: 'status', title: "会议室状态", formatter: (value) => {
                                const item = parseInt(value)
                                return (item === 1)? '可用': (item === 2)? '已有预约': '已停用'
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.handleCommunityState(true);
            $("#c-community_id").change();
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.handleCommunityState();
            $("#c-community_id").change();
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.handleCommunityState();
            $("#c-community_id").change();
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        handleCommunityState: function (showAll) {
            var building_id = $("#building_id").val();
            $("#c-community_id").bind("change",function(){
                var community_code = $(this).val();
                $.ajax({
                    type: "POST",
                    url: 'house/index/get_building_by_cm_code',
                    async: true,
                    cache: false,
                    dataType : "json",
                    data: {community_code:community_code},
                    success: function(data) {
                        var building = data.building;

                        var buildingHtml = showAll ? '<option value="">全部</option>' : '';
                        $.each(building,function(index,item){
                            buildingHtml += '<option value="'+item.code+'">'+item.name+'</option>';
                        });
                        if(buildingHtml == ''){
                            buildingHtml = '<option value="">没有任何选中项</option>';
                        }
                        $("#building_code").html(buildingHtml);
                        if (building_id) {
                            $("#building_code").val(building_id);
                        }
                        $("#building_code").selectpicker({
                            showTick:true,
                            liveSearch:true
                        });
                        $("#building_code").selectpicker("refresh");
                    }
                });
            });
        }
    };
    return Controller;
});